<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">报价名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="quotationName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">报价编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="quotationNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">客人名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customerName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-addQuotation-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-refresh-btn"><i class="layui-icon layui-icon-refresh"></i> 刷新
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加
                </button>
<!--                limit: 5,-->
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createQuotation">导出报价单 </button>-->
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addContract"> 生成订单信息
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="copyQuotation"> 复制报价单
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="export">按模板导出
                </button>
<!--                limit: 5,-->
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="uploadFile">上传客户模板-->
<!--                </button>-->
<!--                limit: 5,-->
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="exportByCustomer">按客户模板导出</button>-->
<!--                limit: 5,-->

<!--                <button type="submit" class="layui-btn  layui-btn-sm layui-btn-sm data-add-btn"-->
<!--                        lay-event="importDemo">-->
<!--                    下载客户模板-->
<!--                </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="show">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    var lay, form, table, util;
    var countNum;
    layui.use(['form', 'table', 'util'], function () {
        lay = layui.jquery;
        form = layui.form;
        util = layui.util;
        table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/quotation/pages',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: {
                param: {}
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, hidden: true, title: 'ID', sort: true},
                {field: 'quotationName', width: 120, title: '报价单名称'},
                {field: 'quotationNo', width: 120, title: '报价单编号'},
                {field: 'customerNo', width: 150, title: '客人编号'},
                {field: 'customerName', width: 150, title: '客人名称'},
                {
                    field: 'quotationTime',
                    width: 120,
                    title: '报价时间',
                    templet: "<div>{{layui.util.toDateString(d.quotationTime, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'currencyCode', width: 80, title: '币别'},
                {field: 'harborFor', width: 140, title: 'FOR港口'},
                {field: 'remark', width: 140, title: '备注'},
                {
                    field: 'createTime',
                    width: 120,
                    title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'updateUser', width: 100, title: '修改人'},
                {
                    field: 'updateTime',
                    width: 120,
                    title: '修改时间',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}</div>"
                },
                {title: '操作', minWidth: 170, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
            ]],
            done: function (res, curr, count) {  //隐藏列
                if ($.common.userIsAdmin()) {
                    $("[data-field='updateUser']").css('display', 'none');
                    $("[data-field='updateTime']").css('display', 'none');
                }
                countNum = res.data.length;
            },
            limits: [5, 10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-addQuotation-btn)', function (data) {
            //执行搜索重载
            search(data)
            return false;
        });

        form.on('submit(data-refresh-btn)', function (data) {
            // 提交刷新了当前页面
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加报价单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/add.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'addContract') {  //生成订单信息
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                let callback = {
                    onSuccess: function (result) {
                        if (result.code == '0') {
                            $.common.showSuccessMessage(result.message);
                            search(data);
                        } else {
                            $.common.showErrorMessage(result.message);
                            return false;
                        }
                    }
                };
                $.xAjax('/quotation/addContractByQuotation/' + id, 'POST', JSON.stringify({}), callback);
                return false;
            } else if (obj.event === 'createQuotation') {
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                window.location.href = '/quotation/createQuotation/' + id;
                return false;
            } else if (obj.event === 'copyQuotation') {
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                let callback = {
                    onSuccess: function (result) {
                        if (result.code == '0') {
                            window.location.reload();
                            $.common.showSuccessMessage(result.message);
                        } else {
                            $.common.showErrorMessage(result.message);
                            return false;
                        }
                    }
                };
                $.xAjax('/quotation/copyQuotation/' + id, 'POST', JSON.stringify({}), callback);

                return false;
            } else if (obj.event === 'uploadFile') {
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let customerId = checkStatus.data[0].customerId;
                var index = layer.open({
                    title: '上传报价单模板',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '95%'],
                    content: '/static/page/quotation/uploadFile.html?customerId=' + customerId,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'exportByCustomer') {  //按客户模板导出
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                url = '/quotation/exportByCustomer/' + id;
                window.location.href = url;
            }else if (obj.event === 'importDemo') {
                // window.location.href = '/static/template/item_import.xlsx';
                window.location.href = '/static/template/quotation_template.xlsx';
            }else if (obj.event === 'export') {
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                var index = layer.open({
                    title: '按模板导出',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '55%'],
                    saveBtn:function(){

                    },
                    content: '/static/page/quotation/selectExport.html?id=' + id,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'show') {
                var index = layer.open({
                    title: '查看报价单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/add.html?quotationId=' + data.id
                        + "&customerId=" + data.customerId
                        + "&contractId=" + data.contractId
                        + "&isShow=true",
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            if ('quotationTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd')
                                body.find("#" + key).val(a);
                            } else {
                                body.find("#" + key).val(data[key]);
                            }

                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改报价单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/add.html?quotationId=' + data.id
                        + "&customerId=" + data.customerId
                        + "&contractId=" + data.contractId,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            if ('quotationTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd')
                                body.find("#" + key).val(a);
                            } else {
                                body.find("#" + key).val(data[key]);
                            }

                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除？', function (index) {
                    let url = "/quotation/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

        $("body").on('click', '.layui-table-box tr ', function () {
            console.log(this);
            console.log(1111111);
            var data_index = $(this).attr('data-index'); //得到当前的tr的index
            console.log($(this).attr('data-index'));
            for (var i = 0; i < countNum; i++)//countNum为总行数
            {
                if (i % 2 == 0) { //设置隔行换色
                    $(".layui-table-body tr[data-index=" + i + "]").attr({"style": "background:#D1EEEE;color:black"}); //恢复原有tr颜色
                } else {
                    $(".layui-table-body tr[data-index=" + i + "]").attr({"style": "background:#fff;color:black"}); //恢复原有tr颜色
                }
            }
            $(".layui-table-body tr[data-index=" + data_index + "]").attr({"style": "background:#1E9FFF;color:#fff"}); //改变当前tr颜色
        });
    });

    function search(data) {
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                param: data.field
            }
        }, 'data');
    }
</script>

</body>
</html>
